/*
 * help_functions.c
 *
 * Created: 20.03.2013 22:12:54
 *  Author: Timmo
 */ 
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <stdlib.h>

#include "timer_functions.h"
#include "adc_drv.h"
#include "sw_drv.h"
#include "adc_drv.h"
#include "display.h"
#include "init.h"

extern uint8_t display_refresh;
extern adc_event_t adc_event;
extern uint8_t mode_value;

uint8_t digits( uint16_t val )
{
	uint8_t i = 1;
	switch( val ){
		default:            i++;
		case 1000 ... 9999: i++;
		case 100 ... 999:   i++;
		case 10 ... 99:     i++;
		case 0 ... 9:       return i;
	}
}

int16_t get_light_level(void *level){
	int16_t adc_val, last_extreme=0;

	lcd_setfontsize(font_medium);

	ADC_set_ch1_pin(4);
	
	do{
				
		adc_val = abs(ADC_read_ch1())/20;
        last_extreme = adc_val;
#if 0        
		if(adc_val > 100)
			adc_val = 100;
		
		if(mode_value == 0){
			if(adc_val < last_extreme){
				last_extreme = adc_val;
		
			}	
		}		
		if(mode_value == 1){
			if(adc_val > last_extreme){
				last_extreme = adc_val;
				
			}
		}
					
		if( rtc_seconds > 2){
			
			last_extreme = trigger_val;
			RTC_reset_timer();
		}
#endif
		if(display_refresh == 1){
			display_refresh=0;
	
			clr_display();
			tiny_printp(6,1,PSTR("Helligkeit: "));
			lcd_putint(last_extreme);
			tiny_printp(6,2,PSTR("Trigger-Level: "));
			lcd_putint(adc_event.threshold);

			lcd_progressbar(6,40,110,20,adc_val);
			lcd_line(adc_event.threshold+8,36,adc_event.threshold+8,63);
			if(adc_event.event_mode == 0){
				lcd_goto(adc_event.threshold+2,4);
				GLCD_Putchar(CHAR_ARROW_LEFT);
			}
			else if(adc_event.event_mode == 1){
				lcd_goto(adc_event.threshold+10,4);
				GLCD_Putchar(CHAR_ARROW_RIGHT);
			}			
			show_display();
		}
		if(get_key_short(1<<KEY_LEFT)){
			adc_event.threshold -= 5;
			if(adc_event.threshold<0)
				adc_event.threshold=0;
		}
		if(get_key_short(1<<KEY_RIGHT)){
			adc_event.threshold+=5;
			if(adc_event.threshold > 100)
				adc_event.threshold = 100;
		}
		//Trigger below level
		if(get_key_short(1<<KEY_DOWN)){
			adc_event.event_mode = 0;
		}
		//Trigger above level
		if(get_key_short(1<<KEY_UP)){
			adc_event.event_mode = 1;
		}
		
		
	}while(!get_key_short(1<<KEY_OK));
	
	
    return 0;
}

int16_t get_sound_level(void *level){
    int16_t adc_val, last_extreme=0;

    
	
    lcd_setfontsize(font_medium);

    ADC_set_ch1_pin(3);
    
    do{
        
        adc_val = abs(ADC_read_ch1()-1024)/10;
        last_extreme = adc_val;
#if 0
        if(adc_val > 100)
        adc_val = 100;
        
        if(mode_value == 0){
            if(adc_val < last_extreme){
                last_extreme = adc_val;
                
            }
        }
        if(mode_value == 1){
            if(adc_val > last_extreme){
                last_extreme = adc_val;
                
            }
        }
        
        if( rtc_seconds > 2){
            
            last_extreme = trigger_val;
            RTC_reset_timer();
        }
        
#endif
        if(display_refresh == 1){
            display_refresh=0;
            
            clr_display();
            tiny_printp(6,1,PSTR("Soundpegel: "));
            lcd_putint(last_extreme);
            tiny_printp(6,2,PSTR("Trigger-Level: "));
            lcd_putint(adc_event.threshold);

            lcd_progressbar(6,40,110,20,adc_val);
            lcd_line(adc_event.threshold+8,36,adc_event.threshold+8,63);
            if(adc_event.event_mode == 0){
                lcd_goto(adc_event.threshold+2,4);
                GLCD_Putchar(CHAR_ARROW_LEFT);
            }
            else if(adc_event.event_mode == 1){
                lcd_goto(adc_event.threshold+10,4);
                GLCD_Putchar(CHAR_ARROW_RIGHT);
            }
            show_display();
        }
        if(get_key_short(1<<KEY_LEFT)){
            adc_event.threshold -= 5;
            if(adc_event.threshold<0)
            adc_event.threshold=0;
        }
        if(get_key_short(1<<KEY_RIGHT)){
            adc_event.threshold+=5;
            if(adc_event.threshold > 100)
            adc_event.threshold = 100;
        }
        //Trigger below level
        if(get_key_short(1<<KEY_DOWN)){
            adc_event.event_mode = 0;
        }
        //Trigger above level
        if(get_key_short(1<<KEY_UP)){
            adc_event.event_mode = 1;
        }
        
        
    }while(!get_key_short(1<<KEY_OK));
    
	
    return 0;
}
int16_t get_camera_type(int16_t *value){
    return 0;	
}	

